<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class User extends Controller{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $a=Session::get('name','thinkphp');
        $id=$a['user_id'];
        $user=Db::table('users')->where("user_id='$id'")->find();
        $this->assign('name',$user);
        return view('User_Personalinfo');
    }

    //用户表单
    public function usershow(){
        return view('User');
    }
    //收货地址
    public function recive(){
        $res=Db::table('province')->where("parent_id=0")->select();
        $this->assign('num',$res);
        return view('user_address');
    }
    //下拉市、县
    public function exercise(){
        $pid=Request::instance()->param('pid');
        $res=Db::table('province')->where("parent_id = $pid")->select();
        echo json_encode($res);
    }
    //购买信息添加
    public function adds(){
        $data=Request::instance()->param();
        $res=Db::table('user_address')->insert($data);
        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function change0(){
        return view('User_Personalinfo');
    }
    //修改
    public function change(){

        $data=Request::instance()->param();
        $img=$this->upload();
        if($img!=''){
            $data['user_images']=$img;
        }
        $data['birthday']=implode('',$data['birthday']);

        $res=Db::table('users')->update($data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }
    public function upload(){
            // 获取表单上传文件 例如上传了001.jpg
        if(request()->file('user_images')){
            $file=request()->file('user_images');
            $info = $file->move(ROOT_PATH . 'public' . DS . 'users');
            if($info){
                return $info->getFilename();
            }else{
                // 上传失败获取错误信息
                return $file->getError();
            }
        }
}
    //修改密码
    public function user_changePassword(){
        return view('user_changePassword');
    }

}
